<template>
  <div class="majorIssues">
    <listTableNew v-bind="tableSetting" ref="klTable"></listTableNew>
  </div>
</template>

<script>
import {
  getListApi //列表
} from "@/api/orderManagement/majorIssues";
export default {
  props: {
    showType: {
      type: String,
      default: ""
    },
    orderCode: {
      type: String,
      default: ""
    },
    selectCode: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      tableSetting: {
        radioShow: false,
        showCustomBtn: true, //是否显示自定义列按钮
        showFilterBtn: true, //是否显示过滤器按钮
        pageConfig: {
          pageCode: "majorIssues", //页面标识，用来存储过滤器以及自定义列的时候用
          userId: this.$store.getters.userInfo.id
        },
        // 查询配置
        searchObj: {
          searchMethod: (params, sortStr) => {
            this.getTableData(params, sortStr);
          },
          queryData: {
            page: 1,
            pageSize: 15
          },
          // 查询条件
          searchArr: [
            {
              type: "input",
              label: "标题",
              model: "title"
            },
            {
              type: "dept",
              label: "申请单位",
              model: "deptName",
              idsStr: "deptId",
              clearable: true
            },
            {
              type: "organization",
              label: "设备所属部门",
              model: "deptManageName",
              idsStr: "deptManage",
              clearable: true
            },
            {
              type: "input",
              label: "工单编号",
              model: "orderCode"
            },
            {
              type: "select",
              label: "状态",
              model: "statusList",
              multiple: true,
              option: this.$store.getters.dirtData("question_Status")
            },
            {
              type: "input",
              label: "编号",
              model: "code"
            },
            {
              type: "userOnly",
              label: "创建人",
              model: "createByName",
              idsStr: "createBy"
            },
            {
              type: "userOnly",
              label: "当前处理人",
              model: "currNodeUser",
              idsStr: "currNodeUserId"
            },
            {
              label: "创建时间",
              model: "createTime",
              type: "datetimerange"
            }
          ]
        },
        // 按钮设置
        actionList: [
          {
            name: "新建",
            icon: "el-icon-plus",
            disabled: !this.$store.getters["isBtnAuth"]("add"),
            content: this.$store.getters["isBtnAuth"]("add")
              ? null
              : "未授权此功能！",
            method: row => {
              this.gotoDetails(row, "majorIssuesDetails", "add");
            }
          }
        ],
        // 表格的行
        column: [
          {
            label: "状态",
            prop: "status",
            sortable: "custom",
            formatter: row => {
              let value = row.status;
              return (
                this.$store.getters["fineDirtDataName"](
                  "question_Status",
                  value
                ) || value
              );
            },
            width: "110px"
          },
          {
            label: "编号",
            prop: "code",
            width: "200px"
          },
          {
            label: "标题",
            prop: "title",
            showTooltip: true,
            width: "320px"
          },
          {
            label: "工单编号",
            prop: "orderCode",
            sortable: "custom",
            minWidth: "210px"
          },
          {
            label: "申请单位",
            prop: "deptName",
            sortable: "custom",
            minWidth: "180px",
            showTooltip: true,
          },
          {
            label: "设备所属部门",
            prop: "deptManageName",
            sortable: "custom",
            minWidth: "180px",
            showTooltip: true,
          },
          {
            label: "当前节点",
            prop: "currNodeName",
            sortable: "custom",
            showTooltip: true,
            minWidth: "180px"
          },
          {
            label: "当前处理人",
            prop: "currNodeUser",
            sortable: "custom",
            minWidth: "180px",
            showTooltip: true,
            // sortable: ""
          },    
          {
            label: "创建人",
            prop: "createByName",
            sortable: "custom",
            minWidth: "90px"
          },
          {
            label: "创建时间",
            prop: "createTime",
            sortable: "custom",
            minWidth: "140px"
          }
        ],
        // 表格数据
        tableData: [],
        //表格方法
        tableMethods: {
          //点击行的方法
          rowClick: row => {
            if (this.pageType == "query") {
              this.$refs.klTable.radioSelect(row);
            } else {
              this.gotoDetails(row, "majorIssuesDetails", "edit");
            }
          }
        }
      }
    };
  },
  created() {
    /* 添加监听事件 */

    window.addEventListener("storage", this.updateData);
  },
  beforeDestroy() {
    /* 删除监听事件 */
    window.removeEventListener("storage", this.updateData);
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.pageType = this.showType;
      if (this.pageType == "query") {
        this.tableSetting.searchObj.searchArr = [
          {
            type: "input",
            label: "工单编号",
            model: "code"
          },
          {
            type: "input",
            label: "工单标题",
            model: "title"
          },
          {
            type: "select",
            label: "工单状态",
            model: "statusList",
            multiple: true,
            option: this.$store.getters.dirtData("question_Status")
          }
        ];
        this.tableSetting.radioShow = true;
        this.tableSetting.showCustomBtn = false;
        this.tableSetting.showFilterBtn = false;
        this.tableSetting.actionList = [];
      } else {
        this.$refs.klTable.getTableConfig();
      }
      setTimeout(() => {
        this.updateData();
      }, 1000);
    },
    updateData() {
      this.$refs.klTable.searchData();
    },
    getTableData(params, sortStr) {
      let form = { ...params };
      if (this.orderCode) {
        form.orderCode = this.orderCode;
      }
      if (sortStr) {
        form.orders = sortStr;
      }
      if (form.createTime) {
        form.startTime = form.createTime[0];
        form.endTime = form.createTime[1];
        delete form.createTime;
      }
      getListApi(form)
        .then(res => {
          let data = res.data.list || [];
          let total = res.data.total || 0;
          this.$set(this.tableSetting, "tableData", data);
          this.$set(this.tableSetting, "totalNumber", total);
          if (this.selectCode) {
            let selectRow = {};
            for (let i in data) {
              if (data[i].code == this.selectCode) {
                selectRow = data[i];
                break;
              }
            }
            this.$refs.klTable.radioSelect(selectRow);
          }
        })
        .catch(e => {
          this.$set(this.tableSetting, "tableData", []);
          this.$set(this.tableSetting, "totalNumber", 0);
        });
    },
    gotoDetails(row, router, type) {
      this.pageType = type;
      let form = {
        pageType: this.pageType
      };
      if (this.pageType != "add") {
        form.id = row.id;
      }
      const { href } = this.$router.resolve({
        name: router,
        query: form
      });
      window.open(href, "_blank");
    },
    //获取选择结果
    getSelectData() {
      let data = this.$refs.klTable.getRadioData();
      return data;
    }
  }
};
</script>

<style lang="less" scoped>
.majorIssues {
  min-height: 500px;
  height: 100%;
}
</style>